package org.compression.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.common.util.JWTHelpUtil;
import org.compression.constant.Constant;

import com.fasterxml.jackson.core.filter.TokenFilter;

public class UserCostFilter implements Filter {



	@Override
	public void init(FilterConfig filterConfig) throws ServletException {


	}


	//token  授权码登录  用户登录 

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		//req.getUri();

		//	System.out.println(req.getRequestURL());

		String ip=request.getRemoteAddr();
		System.out.println(ip);
		/*	
		if(req.getRequestURL().indexOf("auth")!=-1) {
			chain.doFilter(request,response);
		}else {
			String token=req.getHeader("token");
			System.out.println(token);
			try {
				JWTHelpUtil.auth(token);
				chain.doFilter(request,response);
			} catch (Exception e) {
				response.setContentType("text/html; charset=utf8");
				response.getWriter().println("无效请求");
			};
		}*/

		//	request.get  直接授权
		if(Constant.ips.contains(ip)) {
			chain.doFilter(request,response);

			/*if(req.getRequestURL().indexOf("auth")!=-1) {
				chain.doFilter(request,response);
			}else {
				String token=req.getHeader("token");
				System.out.println(token);
				try {
					JWTHelpUtil.auth(token);
					chain.doFilter(request,response);
				} catch (Exception e) {
					response.setContentType("text/html; charset=utf8");
					response.getWriter().println("无效请求");
				};
			}*/		


			//授权用户
		}else if(req.getHeader("token")!=null){
			String token=req.getHeader("token");
			try {
				JWTHelpUtil.auth(token);
				chain.doFilter(request,response);
			} catch (Exception e) {
				response.setContentType("text/html; charset=utf8");
				response.getWriter().println("鉴权失败");
			};
		}else if(req.getHeader("bussineToken")!=null){
			String token=req.getHeader("token");	

		}else {
			response.setContentType("text/html; charset=utf8");
			response.getWriter().println("未经授权");
		}















	}

	@Override
	public void destroy() {


	}









}
